package com.cskaoyan.util;

import com.cskaoyan.bean.Storage;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.UUID;

/**
 * @author xudengfeng
 * @date 2020/12/7 15:33
 */


public class StorageUtils {
    public static Storage setStorageInfo(MultipartFile file) {
        Storage storage = new Storage();
        //上传图片时间
        Date time = new Date();
        UUID uuid = UUID.randomUUID();
        String contentType = file.getContentType();
        String filename = file.getOriginalFilename();

        int i = filename.lastIndexOf(".");//需要取得最后一个.的序号，因为文件名可能包含.
        String fileSuffix = filename.substring(i + 1);
        storage.setKey(uuid + "." + fileSuffix);
        storage.setName(filename);
        storage.setType(contentType);
        storage.setSize((int) file.getSize());
        storage.setAddTime(time);
        storage.setUpdateTime(time);
        storage.setDeleted(false);
        return storage;
    }
}
